#include <malloc.h>
#include "10_lib.h"
nodep* InitStack(void)
{
	nodep* p = (nodep*)malloc(sizeof(nodep));
	*p = NULL;
	return p;
}
void freeNode(nodep p)
{
	if(p->next != NULL)
	{
		freeNode(p->next);
	}
	free(p);
}
void SetEmptyStack(nodep* p)
{
	if(IsEmptyStack(p) == 1)
	{
		return;
	}
	freeNode(*p);
}
int IsEmptyStack(nodep* p)
{
	if(*p == NULL)
	{
		return 1;
	}
	return 0;
}
void StackPush(nodep* p,int data)
{
	nodep temp = (nodep)malloc(sizeof(node));
	temp->data = data;
	temp->next = *p;
	*p = temp;
}
int StackPop(nodep* p)
{
	if(IsEmptyStack(p) == 1)
	{
		return 0;
	}
	int data = (*p)->data;
	nodep temp = *p;
	*p = (*p)->next;
	free(temp);
	return data;
}
int ShowTop(nodep* p)
{
	if(IsEmptyStack(p) == 1)
	{
		return 0;
	}
	return (*p)->data;
}
int StackLength(nodep* p)
{
	if(IsEmptyStack(p) == 1)
	{
		return 0;
	}
	nodep temp = *p;
	int length = 0;
	while(temp != NULL)
	{
		length++;
		temp = temp->next;
	}
	return length;
}

queuep InitQueue(void)
{
	queuep q = (queuep)malloc(sizeof(queue));
	q->front = NULL;
	q->rear = NULL;
	return q;
}
int IsEmptyQueue(queuep q)
{
	return (q->front == NULL) ? 1 : 0;
}
void SetEmptyQueue(queuep q)
{
	nodep n = q->front,temp = NULL;
	while(n != NULL)
	{
		temp = n;
		n = n->next;
		free(temp);
	}
	q->front = NULL;
	q->rear = NULL;
}
int IsFull(queuep q)
{
	return 0;
}
void PushQueue(queuep q,int v)
{
	if(q->rear == NULL)
	{
		q->rear = (nodep)malloc(sizeof(node));
		q->front = q->rear;
		q->rear->next = NULL;
	}
	else
	{
		q->rear->next = (nodep)malloc(sizeof(node));
		q->rear->next->next = NULL;
		q->rear = q->rear->next;
	}
	q->rear->data = v;
}
int OutQueue(queuep q)
{
	if(IsEmptyQueue(q) == 1)
	{
		return -1;
	}
	int n = q->front->data;
	nodep temp = q->front;
	q->front = q->front->next;
	if(q->front == NULL)
	{
		q->rear = NULL;
	}
	free(temp);
	return n;
}
int ShowFront(queuep q)
{
	if(IsEmptyQueue(q) == 1)
	{
		return -1;
	}
	return q->front->data;
}
int QueueLength(queuep q)
{
	int length = 0;
	if(IsEmptyQueue(q) != 1)
	{
		nodep n = q->front;
		while(n != NULL)
		{
			n = n->next;
			length++;
		}
	}
	return length;
}
void PrintQueue(queuep q)
{
	if(IsEmptyQueue(q) != 1)
	{
		nodep n = q->front;
		while(n != NULL)
		{
			printf("%d\n",n->data);
			n = n->next;
		}
	}
}
